package com.tcm.doctor.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.base.core.dozer.DozerConvert;
import com.base.core.lang.DateUtils;
import com.base.core.lang.LocalDateTimeUtils;
import com.base.core.pager.PageBean;
import com.base.core.pager.PageContext;
import com.base.core.utils.R;
import com.base.genid.generator.UidGenerator;
import com.tcm.doctor.domain.dto.DoctorFlowDto;
import com.tcm.doctor.domain.dto.DoctorFlowPageDto;
import com.tcm.doctor.domain.entity.DoctorAccount;
import com.tcm.doctor.domain.entity.DoctorFlow;
import com.tcm.doctor.domain.enums.BusinessCodeEnum;
import com.tcm.doctor.domain.vo.DoctorFlowVo;
import com.tcm.doctor.mapper.DoctorAccountMapper;
import com.tcm.doctor.mapper.DoctorFlowMapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;

@DubboService(version = "0.0.1-alpha", retries = 3, timeout = 6000, delay = -1)
public class DoctorFlowServiceImpl implements DoctorFlowService {

    @Autowired
    private DoctorFlowMapper doctorFlowMapper;

    @Autowired
    private DoctorAccountMapper doctorAccountMapper;

    @DubboReference(version = "0.0.1-beta", group = "cached-uid", timeout = 6000, check = false, retries = 3)
    private UidGenerator uidGenerator;

    /**
     * 获得单条信息
     *
     * @param id
     * @return Doctor
     */
    @Override
    public DoctorFlowVo findById(Long id) {
        DoctorFlow doctorFlow = doctorFlowMapper.findById(id);
        DoctorFlowVo doctorFlowVo = null;
        if (ObjectUtil.isNotEmpty(doctorFlow)) {
            doctorFlowVo = DozerConvert.copyProperties(doctorFlow, DoctorFlowVo.class);
        }
        return doctorFlowVo;
    }


    /**
     * 新增信息
     *
     * @param doctorFlowDto
     */
    @Override
    public void save(DoctorFlowDto doctorFlowDto) throws Exception {
        //判断id是否为空
        if (doctorFlowDto.getUserId().equals(0L)) {
            R.throwFail(BusinessCodeEnum.DOCTOR_USER_ID_NOT_EXISTS);
        }
        //判断类型是否为空
        if (ObjectUtil.isEmpty(doctorFlowDto.getType())) {
            R.throwFail(BusinessCodeEnum.Type_NOT_EXISTS);
        }
        //判断金额是否为空
        if (ObjectUtil.isEmpty(doctorFlowDto.getMoney())) {
            R.throwFail(BusinessCodeEnum.MONEY_NOT_EXISTS);
        }
        DoctorFlow doctorFlow = DozerConvert.copyProperties(doctorFlowDto, DoctorFlow.class);
        doctorFlow.setUpdateTime(LocalDateTime.now());
        doctorFlow.setId(uidGenerator.getUID());
        doctorFlow.setCreateTime(LocalDateTime.now());
        doctorFlowMapper.insert(doctorFlow);
    }


    /**
     * 永久删除
     *
     * @param id
     */
    @Override
    public void deleteDoctor(Long id) {
        doctorFlowMapper.deleteById(id);
    }

    /**
     * 删除到回收站
     *
     * @param id
     */
    @Override
    public void deleteLogicDoctor(Long id) {
        doctorFlowMapper.deleteLogicById(id);
    }

    /**
     * 按时间段查询
     *
     * @param doctorFlowPageDto
     * @return
     */
    @Override
    public PageBean<DoctorFlowVo> listPage(DoctorFlowPageDto doctorFlowPageDto) {
        PageContext.setCurrentPage(doctorFlowPageDto.getPage());
        PageContext.setPageSize(doctorFlowPageDto.getPagesize());
        doctorFlowPageDto.setStart(PageContext.getStart());

        //如果没又给定月份则默认为当前月份
        if (doctorFlowPageDto.getYear() == null) {
            //获取当月的第一天和最后一天的日期
            doctorFlowPageDto.setStartTime(DateUtils.getFirstDayOfMonth());
            doctorFlowPageDto.setEndTime(DateUtils.getLastDayOfMonth());
        } else {
            doctorFlowPageDto.setStartTime(DateUtils.getFirstDayOfMonth(doctorFlowPageDto.getYear(), doctorFlowPageDto.getMonth()));
            doctorFlowPageDto.setEndTime(DateUtils.getLastDayOfMonth(doctorFlowPageDto.getYear(), doctorFlowPageDto.getMonth()));
        }

        List<DoctorFlow> doctorFlowList = doctorFlowMapper.listPage(doctorFlowPageDto);
        List<DoctorFlowVo> doctorFlowVoList = null;
        if (ObjectUtil.isNotNull(doctorFlowList)) {
            doctorFlowVoList = DozerConvert.copyList(doctorFlowList, DoctorFlowVo.class);
            for (DoctorFlowVo doctorFlowVo : doctorFlowVoList) {
                doctorFlowVo.setCreateTimeText(DateUtil.formatDateTime(LocalDateTimeUtils.localDateTimeToDate(doctorFlowVo.getCreateTime())));
            }
        }

        Long count = doctorFlowMapper.count(doctorFlowPageDto);
        PageBean<DoctorFlowVo> doctorFlowVoPageBean = new PageBean<DoctorFlowVo>(doctorFlowVoList, count, doctorFlowPageDto.getStart(), doctorFlowPageDto.getPagesize());
        return doctorFlowVoPageBean;
    }

    @Override
    public void insertRecord(DoctorFlow doctorFlow) {
        doctorFlow.setCreateTime(LocalDateTime.now());
        doctorFlow.setUpdateTime(LocalDateTime.now());
        doctorFlow.setDeleted(new Byte("0"));
        doctorFlowMapper.insert(doctorFlow);

        //添加账户余额
        DoctorAccount doctorAccount = doctorAccountMapper.findByUserId(doctorFlow.getUserId());
        if (ObjectUtil.isNotNull(doctorAccount)) {
            doctorAccount.setCommission(NumberUtil.add(doctorFlow.getMoney(), doctorAccount.getCommission()));
            doctorAccount.setCurrentMoney(NumberUtil.add(doctorFlow.getMoney(), doctorAccount.getCurrentMoney()));
            doctorAccount.setTotalMoney(NumberUtil.add(doctorFlow.getMoney(), doctorAccount.getTotalMoney()));
            doctorAccount.setUpdateTime(LocalDateTime.now());
            doctorAccountMapper.update(doctorAccount);
        } else {
            DoctorAccount docAccount = new DoctorAccount();
            docAccount.setId(uidGenerator.getUID());
            docAccount.setUserId(doctorFlow.getUserId());
            docAccount.setCurrentMoney(doctorFlow.getMoney());
            docAccount.setCommission(doctorFlow.getMoney());
            docAccount.setTotalMoney(doctorFlow.getMoney());
            docAccount.setCreateTime(LocalDateTime.now());
            docAccount.setUpdateTime(LocalDateTime.now());
            doctorAccountMapper.insert(docAccount);
        }


    }

}

